package com.itheima.edu.info.manager.controller;

import com.itheima.edu.info.manager.domain.Student;
import com.itheima.edu.info.manager.domain.Teacher;
import com.itheima.edu.info.manager.sercive.TeacherService;

import java.util.Scanner;

public class TeacherController {
    TeacherService teacherService = new TeacherService();
    private Scanner sc = new Scanner(System.in);
    public void teacherStart() {
        studentLoop:while(true) {
            System.out.println("--------欢迎使用黑马<教师>管理系统--------");
            System.out.println("请输入您的选择:1.添加老师 2.删除老师 3.修改老师  4.查询老师 5.退出");
            String s = sc.next();
            switch (s) {
                case "1":
                    //System.out.println("添加老师");
                    addTeacher();
                    break;
                case "2":
                    //System.out.println("删除老师");
                    //deleteStudentById();
                    break;
                case "3":
                    //System.out.println("修改老师");
                    //updateTeacher();
                    break;
                case "4":
                    //System.out.println("查询老师");
                    findAllTeacher();
                    break;
                case "5":
                    System.out.println("感谢您使用老师管理系统");
                    //return; //return是跳出while循环
                    break studentLoop;//使用此方法时,一定要在while前做一个标记(标记名可以随便起),此处跳出标记的while循环
                //以上两种方法都可以
                //break;//此处不能用break,break是跳出switch语句
                default:
                    System.out.println("您的输入有误,请重新输入!");
            }
        }
    }
    //查询教师
    public void findAllTeacher() {
        Teacher[] teachers=teacherService.findAllTeacher();
        //判断数组是否为null
        if (teachers==null){
            System.out.println("没有教师信息,请添加后重试");
            return;//退出当前
        }
        //打印表头信息
        System.out.println("学号\t\t姓名\t\t年龄\t\t生日");
        //不为空,遍历数组并打印教师信息
        for (int i = 0; i < teachers.length; i++) {
            Teacher t = teachers[i];
            if (t!=null){
                System.out.println(t.getId()+"\t"+t.getName()+"\t"+t.getAge()+"\t"+t.getBirthday());
            }
        }
    }

    //添加教师
    public void addTeacher() {
        Scanner sc = new Scanner(System.in);
        String id;
        while (true) {
            //键盘输入教师信息
            System.out.println("请输入教师id:");
            id = sc.next();
            //2.判断教师id是否存在,如果不存在,则继续录入
            boolean exists = teacherService.isExists(id);
            if (exists) {
                System.out.println("教师id存在,请重新输入!");
            }else{
                break;
            }
        }
        System.out.println("请输入教师姓名:");
        String name = sc.next();
        System.out.println("请输入教师年龄:");
        int age = sc.nextInt();
        System.out.println("请输入教师生日:");
        String birthday = sc.next();
        //创建教师对象并赋值
        Teacher teacher = new Teacher();
        teacher.setId(id);
        teacher.setName(name);
        teacher.setAge(age);
        teacher.setBirthday(birthday);
        //调用业务员把教师对象传到仓管
        boolean result = teacherService.addTeacher(teacher);
        //根据获得的结果判断是否添加成功
        if (result){
            System.out.println("添加成功");
        }else{
            System.out.println("添加失败");
        }



    }
}
